package com.tiancheng.trade.merchant.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.merchant.dto.requestdto.MerchantSettleRuleRequestDTO;
import com.tiancheng.trade.merchant.model.MerchantSettleRule;
import com.tiancheng.trade.merchant.service.IMerchantSettleRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商户清分规则管理
 *
 * @author kellen
 * @date 2021/7/15
 */
@RequestMapping("/admin/merchantSettleRule")
@RestController
public class MerchantSettleRuleAdminController {
    @Autowired
    private IMerchantSettleRuleService merchantSettleRuleService;

    /**
     * 增 - 新增即生效，同时把旧规则失效
     *
     * @param rule {@link MerchantSettleRule}
     */
    @PostMapping("/")
    public Result<?> add(@Validated @RequestBody MerchantSettleRule rule) {
        if (merchantSettleRuleService.addRule(rule)) {
            return Result.success();
        }
        return Result.fail("操作失败");
    }

    @GetMapping("/{id}")
    public Result<MerchantSettleRule> getById(@PathVariable Long id) {
        return Result.success(merchantSettleRuleService.getById(id));
    }


    @DeleteMapping("/{code}")
    public Result<String> deleteByCode(@PathVariable String code) {
        merchantSettleRuleService.deleteByCode(code);
        return Result.success("作废成功");
    }

    /**
     * 条件查询
     *
     * @param ruleRequestDTO {@link MerchantSettleRule}
     * @return {@link Result}<{@link List}<{@link MerchantSettleRule}>>
     */
    @GetMapping("/")
    public Result<PageInfo<MerchantSettleRule>> getByCondition(MerchantSettleRuleRequestDTO ruleRequestDTO) {
        return Result.success(merchantSettleRuleService.queryPage(ruleRequestDTO));
    }


}
